package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 品牌信息控制器
 *
 * @author gaofeng
 * @date 2025年04月18日 13:58
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @GetMapping(value = "/selectAllBrands")
    public Result<List<Brand>> selectAllBrands() {
        return new Result<>(true, StatusCode.OK, "查询成功", brandService.selectAllBrands());
    }

    @GetMapping(value = "/selectBrandById/{id}")
    public Result<Brand> selectBrandById(@PathVariable(value = "id") Integer id) {
        return new Result<>(true, StatusCode.OK, "查询成功", brandService.selectBrandById(id));
    }


    @PostMapping(value = "/insert")
    public Result<Brand> insert(@RequestBody Brand brand) {
        brandService.insert(brand);
        return new Result<>(true, StatusCode.OK, "新增成功", brand);
    }

    @PutMapping(value = "/update")
    public Result<Brand> update(@RequestBody Brand brand) {
        brandService.update(brand);
        return new Result<>(true, StatusCode.OK, "修改成功", brand);
    }

    @DeleteMapping(value = "/delete/{id}")
    public Result<Brand> delete(@PathVariable(value = "id") Integer id) {
        brandService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功", id);
    }

    @PostMapping(value = "/search")
    public Result<List<Brand>> search(@RequestBody Brand brand) {
        return new Result<>(true, StatusCode.OK, "条件查询品牌集合成功", brandService.findList(brand));
    }

    @GetMapping(value = "/findPage/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page") Integer page,
                                            @PathVariable(value = "size") Integer size) {
        return new Result<>(true, StatusCode.OK, "分页查询品牌集合成功", brandService.findPage(page, size));
    }

    @PostMapping(value = "/findBrandByPage/{page}/{size}")
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page") Integer page,
                                            @PathVariable(value = "size") Integer size, @RequestBody Brand brand) {
        return new Result<>(true, StatusCode.OK, "分页条件查询品牌集合成功", brandService.findPage(page, size, brand));
    }

    @GetMapping(value = "/findByCategory/{categoryId}")
    public Result<List<Brand>> findByCategory(@PathVariable(value = "categoryId") Integer categoryId) {
        return new Result<>(true, StatusCode.OK, "根据分类id查询品牌集合成功", brandService.findByCategoryId(categoryId));
    }
}
